{% extends 'base.html' %}
{% load paper_filters %}

{% block title %}Search Results - Research Paper Analyzer{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <!-- Header with Export Options -->
        <div class="card mb-4">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">
                            <i class="fas fa-search me-2"></i>Search Results
                        </h3>
                        <p class="text-muted mb-0">
                            {% if search_query %}
                                Query: <strong>"{{ search_query.query }}"</strong> | 
                            {% endif %}
                            Papers found: <strong>{{ total_papers }}</strong>
                        </p>
                    </div>
                    <div class="btn-group">
                        <a href="{% url 'papers:export_csv' %}" class="btn btn-outline-success">
                            <i class="fas fa-file-csv me-1"></i>Export CSV
                        </a>
                        <!-- <a href="{% url 'papers:export_json' %}" class="btn btn-outline-info">
                            <i class="fas fa-file-code me-1"></i>Export JSON
                        </a> -->
                        <a href="{% url 'papers:search' %}" class="btn btn-outline-primary">
                            <i class="fas fa-search me-1"></i>New Search
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Source Statistics -->
        {% if source_stats %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-chart-bar me-2"></i>Search Statistics
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% for source, count in source_stats.items %}
                    <div class="col-md-3 text-center">
                        <div class="display-6 text-primary">{{ count }}</div>
                        <p class="text-muted">{{ source|title }}</p>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Papers List -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-file-alt me-2"></i>Analyzed Papers
                </h5>
            </div>
            <div class="card-body">
                {% if page_obj.object_list %}
                    <div class="row">
                        {% for paper in page_obj.object_list %}
                            <div class="col-lg-6 mb-4">
                                <div class="card h-100 border">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0 text-primary">{{ paper.title|truncatechars:80 }}</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="row mb-2">
                                            <div class="col-6">
                                                <small class="text-muted">
                                                    <i class="fas fa-users me-1"></i>
                                                    {{ paper.authors|truncatechars:40 }}
                                                </small>
                                            </div>
                                            <div class="col-6 text-end">
                                                <small class="text-muted">
                                                    <i class="fas fa-calendar me-1"></i>
                                                    {{ paper.publication_date.year|default:"N/A" }}
                                                </small>
                                            </div>
                                        </div>
                                        
                                        <p class="card-text small">
                                            {{ paper.abstract|truncatewords:30 }}
                                        </p>
                                        
                                        <!-- Analysis Information -->
                                        {% if paper.analysis %}
                                        <div class="mb-2">
                                            <strong class="small text-success">Key Findings:</strong>
                                            <p class="small mb-1">{{ paper.analysis.key_findings|truncatewords:20 }}</p>
                                        </div>
                                        
                                        {% if paper.analysis.methodology %}
                                        <div class="mb-2">
                                            <strong class="small text-info">Methodology:</strong>
                                            <p class="small mb-1">{{ paper.analysis.methodology|truncatewords:15 }}</p>
                                        </div>
                                        {% endif %}
                                        {% endif %}
                                        
                                        <div class="mt-auto d-flex justify-content-between align-items-center">
                                            <div>
                                                <span class="badge bg-secondary">{{ paper.source|default:"Unknown" }}</span>
                                                {% if paper.citation_count %}
                                                    <span class="badge bg-info">{{ paper.citation_count }} citations</span>
                                                {% endif %}
                                            </div>
                                            <div>
                                                <a href="{% url 'papers:detail' paper.id %}" class="btn btn-sm btn-outline-primary">
                                                    <i class="fas fa-eye"></i> View
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                    
                    <!-- Pagination -->
                    {% if page_obj.has_other_pages %}
                    <nav aria-label="Search results pagination">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">Previous</a>
                                </li>
                            {% endif %}
                            
                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            
                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">Next</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-search fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">No papers found</h5>
                        <p class="text-muted">Try adjusting your search query or parameters.</p>
                        <a href="{% url 'papers:search' %}" class="btn btn-primary">
                            <i class="fas fa-search me-1"></i>Try New Search
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- Recent Searches -->
        {% if search_queries %}
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-history me-2"></i>Recent Searches
                </h5>
            </div>
            <div class="card-body">
                {% for query in search_queries %}
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <div>
                            <strong>{{ query.query|truncatechars:60 }}</strong>
                            <small class="text-muted d-block">{{ query.created_at|timesince }} ago • {{ query.results_count }} results</small>
                        </div>
                        <button class="btn btn-sm btn-outline-primary" onclick="window.location.href='{% url 'papers:search_results' %}?search_id={{ query.id }}'">
                            View Results
                        </button>
                    </div>
                    {% if not forloop.last %}<hr>{% endif %}
                {% endfor %}
            </div>
        </div>
        {% endif %}

        <!-- Export Section -->
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-download me-2"></i>Export Results
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h6>CSV Export</h6>
                        <p class="small text-muted">Download all paper data in spreadsheet format for further analysis.</p>
                        <a href="{% url 'papers:export_csv' %}" class="btn btn-success">
                            <i class="fas fa-file-csv me-1"></i>Download CSV
                        </a>
                    </div>
                    <div class="col-md-6">
                        <h6>JSON Export</h6>
                        <p class="small text-muted">Download complete analysis data in JSON format for programmatic use.</p>
                        <!-- <a href="{% url 'papers:export_json' %}" class="btn btn-info">
                            <i class="fas fa-file-code me-1"></i>Download JSON
                        </a> -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // Add click handlers for paper cards
    $('.card').hover(
        function() {
            $(this).addClass('shadow-sm');
        },
        function() {
            $(this).removeClass('shadow-sm');
        }
    );
    
    // Initialize tooltips
    $('[data-bs-toggle="tooltip"]').tooltip();
});
</script>
{% endblock %} 